
Public Class UserPrivilege

    Protected _user As New User()
    Protected _privilege As New Privilege()
    Protected _create_user As String
    Protected _create_date As DateTime
    Protected _Data As ArrayList
    Protected _DataCurrent As Integer

    Public Sub New()

    End Sub

    Public Sub New(ByVal inUsername As String)
        _Data = New ArrayList
        _DataCurrent = 0
        _Data = getUserPrivilege(inUsername)
    End Sub

    Public Property User() As User
        Get
            Return _user
        End Get
        Set(ByVal value As User)
            _user = value
        End Set
    End Property

    Public Property Privilege() As Privilege
        Get
            Return _privilege
        End Get
        Set(ByVal value As Privilege)
            _privilege = value
        End Set
    End Property

    Public Property create_user() As String
        Get
            Return _create_user
        End Get
        Set(ByVal value As String)
            _create_user = value
        End Set
    End Property

    Public Property create_date() As DateTime
        Get
            Return _create_date
        End Get
        Set(ByVal value As DateTime)
            _create_date = value
        End Set
    End Property

    Private Function getUserPrivilege(ByVal inUsername As String) As ArrayList
        Dim outResult As New ArrayList()
        Dim decryptText As New DateUtilities.Encrypt_Data()
        Dim objConn As New MySql.Data.MySqlClient.MySqlConnection( _
            decryptText.getDecrypt(System.Configuration.ConfigurationManager.ConnectionStrings( _
            "PoliceHealthConnectionString").ToString(), True))
        Dim objCmd As New MySql.Data.MySqlClient.MySqlCommand()
        objCmd.Connection = objConn
        objCmd.CommandType = CommandType.StoredProcedure
        objCmd.CommandText = "proc_SELECT_UserPrivilege"

        Dim objParam As New MySql.Data.MySqlClient.MySqlParameter("inUsername", inUsername)
        objCmd.Parameters.Add(objParam)
        Dim objDa As New MySql.Data.MySqlClient.MySqlDataAdapter(objCmd)
        Dim objDs As New System.Data.DataSet()
        objDa.Fill(objDs, "tableUserPrivilege")

        For Each objDr As System.Data.DataRow In objDs.Tables("tableUserPrivilege").Rows
            Dim obj As UserPrivilege = New PoliceHealthClass.UserPrivilege()
            obj.User.citizen_id = objDr.Item("citizen_id").ToString()
            obj.User.username = objDr.Item("username").ToString()
            obj.Privilege.privilege_id = objDr.Item("privilege_id").ToString()
            obj.Privilege.privilege_name_tha = objDr.Item("privilege_name_tha").ToString()
            obj.Privilege.privilege_name_eng = objDr.Item("privilege_name_eng").ToString()
            obj.Privilege.description = objDr.Item("description").ToString()
            obj.User.prename.prename_id = objDr.Item("prename_id").ToString()
            obj.User.prename.prename_short_tha = objDr.Item("prename_short_tha").ToString()
            obj.User.prename.prename_short_eng = objDr.Item("prename_short_eng").ToString()
            obj.User.prename.prename_full_tha = objDr.Item("prename_full_tha").ToString()
            obj.User.prename.prename_full_eng = objDr.Item("prename_full_eng").ToString()
            obj.User.name = objDr.Item("name").ToString()
            obj.User.surname = objDr.Item("surname").ToString()
            outResult.Add(obj)
        Next

        Return outResult
    End Function

    Public Function deleteUserPrivilege(ByVal inCitizenID As String) As Boolean
        Try
            Dim decryptText As New DateUtilities.Encrypt_Data()
            Dim objConn As New MySql.Data.MySqlClient.MySqlConnection( _
                decryptText.getDecrypt(System.Configuration.ConfigurationManager.ConnectionStrings( _
                "PoliceHealthConnectionString").ToString(), True))
            Dim objCmd As New MySql.Data.MySqlClient.MySqlCommand()
            objCmd.Connection = objConn
            objCmd.CommandType = CommandType.StoredProcedure
            objCmd.CommandText = "proc_DELETE_UserPrivilege"

            Dim objParam As New MySql.Data.MySqlClient.MySqlParameter("inCitizenID", _
                Utilities.SendToDatabase(Utilities.ConvertCitizenID(inCitizenID)))
            objCmd.Parameters.Add(objParam)

            objConn.Open()
            objCmd.ExecuteNonQuery()
            objConn.Close()

            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Public Function insertUserPrivilege(ByVal inUserPrivilege As UserPrivilege) As Boolean
        Try
            Dim decryptText As New DateUtilities.Encrypt_Data()
            Dim objConn As New MySql.Data.MySqlClient.MySqlConnection( _
                decryptText.getDecrypt(System.Configuration.ConfigurationManager.ConnectionStrings( _
                "PoliceHealthConnectionString").ToString(), True))
            Dim objCmd As New MySql.Data.MySqlClient.MySqlCommand()
            objCmd.Connection = objConn
            objCmd.CommandType = CommandType.StoredProcedure
            objCmd.CommandText = "proc_INSERT_UserPrivilege"

            Dim objParam As New MySql.Data.MySqlClient.MySqlParameter("inCitizenID", _
                Utilities.SendToDatabase(Utilities.ConvertCitizenID(inUserPrivilege.User.citizen_id)))
            objCmd.Parameters.Add(objParam)

            objParam = New MySql.Data.MySqlClient.MySqlParameter("inPrivilegeID", _
                Utilities.SendToDatabase(inUserPrivilege.Privilege.privilege_id))
            objCmd.Parameters.Add(objParam)

            objParam = New MySql.Data.MySqlClient.MySqlParameter("inCreateUser", _
                Utilities.SendToDatabase(inUserPrivilege.create_user))
            objCmd.Parameters.Add(objParam)


            objConn.Open()
            objCmd.ExecuteNonQuery()
            objConn.Close()

            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Public Function NextUser() As Boolean
        If ((Not _Data Is Nothing) AndAlso (_DataCurrent < _Data.Count And _Data.Count > 0)) Then
            _user.citizen_id = _Data(_DataCurrent).user.citizen_id
            _user.username = _Data(_DataCurrent).user.username
            _privilege.privilege_id = _Data(_DataCurrent).privilege.privilege_id
            _privilege.privilege_name_tha = _Data(_DataCurrent).privilege.privilege_name_tha
            _privilege.privilege_name_eng = _Data(_DataCurrent).privilege.privilege_name_eng
            _privilege.description = _Data(_DataCurrent).privilege.description
            _user.prename.prename_id = _Data(_DataCurrent).user.prename.prename_id
            _user.prename.prename_short_tha = _Data(_DataCurrent).user.prename.prename_short_tha
            _user.prename.prename_short_eng = _Data(_DataCurrent).user.prename.prename_short_eng
            _user.prename.prename_full_tha = _Data(_DataCurrent).user.prename.prename_full_tha
            _user.prename.prename_full_eng = _Data(_DataCurrent).user.prename.prename_full_eng
            _user.name = _Data(_DataCurrent).user.name
            _user.surname = _Data(_DataCurrent).user.surname
            _DataCurrent += 1
            Return True
        End If
        Return False
    End Function

    Public Sub LoadUserPrivilege(ByVal inUsername As String)
        _Data = New ArrayList
        _DataCurrent = 0
        _Data = getUserPrivilege(inUsername)
    End Sub

End Class
